/** 
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
* 
* MpmProjectDemandService
* 项目需求
* 
* 修改纪录
* 2021-08-04 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2021-08-04
*/
package com.topscomm.mpm.service.impl;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectDemandService;
import com.topscomm.mpm.service.impl.auto.MpmProjectDemandServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mpm.pojo.MpmProjectDemandEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service(value = "mpmProjectDemandService")
@Scope(value = "singleton")
public class MpmProjectDemandService extends MpmProjectDemandServiceAuto implements IMpmProjectDemandService {
	/**
	 * @author: houyuhang
	 * @date: 2021-08-04
	 * @param entity
	 * @description:单号生成
	 * @modify:
	 */
	@Autowired
	DatacodeCache datacodeCache;
	@Override
	protected void beforeInsert(BasicEntity entity){		
		super.beforeInsert(entity);
		GenerateIDAndCode(entity, "PD" , MpmProjectDemandEntity.FieldOrdercode);
//		if (entity.getId() <= 0) {// 赋值Id和单号
//			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
//			entity.setId(arrayId[0]);
//			String docNo = String.valueOf(arrayId[0]);
//			docNo = docNo.substring(0, 6) + docNo.substring(9);
//			entity.setValue(MpmProjectDemandEntity.FieldDocno, docNo);
//			//赋值编号
//			MpmProjectDemandEntity mpmProjectDemandEntity = (MpmProjectDemandEntity) entity;
//			String projectCode = "PD";
//			String projectLevel = mpmProjectDemandEntity.getProjectlevel();
//			projectLevel = datacodeCache
//					.getValueDynamic("MpmProjectLevel", projectLevel, "code", "name");
//			SimpleDateFormat sdFormat = new SimpleDateFormat("yyMMdd");
//			String dateCode = sdFormat.format(new Date());
//			String flowCode = docNo.substring(docNo.length() - 3);
//			projectCode = projectCode + projectLevel + dateCode + flowCode;
//			entity.setValue(MpmProjectDemandEntity.FieldOrdercode, projectCode);
//		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2021-09-11
	 * @param entity
	 * @param sessionUserBean
	 * @description:docno和ordercode生成
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(TapEntity entity, SessionUserBean sessionUserBean) {
		super.beforeSubmit(entity, sessionUserBean);
		GenerateIDAndCode(entity, "PD" , MpmProjectDemandEntity.FieldOrdercode);
	}

	/**
	 * @author: houyuhang
	 * @date: 2021-08-05
	 * @param mapRow
	 * @param datacodeCache
	 * @description:重写setDispFields
	 * @modify:
	 */
	@Override
	public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) {
		super.setDispFields(mapRow, datacodeCache);
		//配置分发人员工号字段显示值
		if (mapRow.get("distribute") != null) {
			String temp = mapRow.get("distribute").toString();
			mapRow.put("dispdistributecode",temp.substring(1,temp.length()-1));
		}
		//配置分发人员字段显示值
		if (mapRow.get("distribute") != null) {
			String temp = mapRow.get("distribute").toString();
			if(temp.length() > 0) {
				temp = temp.substring(1);
			}
			String[] sp = temp.split(",");
			StringBuffer sb = new StringBuffer();
			for(String str : sp){
				sb.append(datacodeCache.getValueDynamic("CboUser", str, "code", "name") + ",");
			}
			String strdis = ConvertUtil.convertToString(sb);
			if(strdis.length()>0)
			{mapRow.put("spdistribute",strdis.substring(0,strdis.length()-1));}
		}
		//配置责任人字段显示值
		if (mapRow.get("incharge") != null) {
			String temp = mapRow.get("incharge").toString();
			String[] sp = temp.split(",");
			StringBuffer sb = new StringBuffer();
			for(String str : sp){
				sb.append(datacodeCache.getValueDynamic("CboUser", str, "code", "name") + ",");
			}
			String strdis = ConvertUtil.convertToString(sb);
			if(strdis.length()>0)
			{mapRow.put("spincharge",strdis.substring(0,strdis.length()-1));}
		}
		//配置项目等级显示值
		if (mapRow.get("projectlevel") != null) {
			String temp = mapRow.get("projectlevel").toString();
			mapRow.put("dispprojectlevel", datacodeCache.getValueDynamic("MpmProjectLevel", temp, "code", "name"));
		}
	}

	/***
	 * @description: 责任人前后添加逗号
	 * @param map
	 * @return
	 * @author: jiangtao1
	 * @date: 2021/12/2 16:23
	 * @modify:
	 */
	@Override
	protected void updateBefore(Map<String, Object> map) throws ServiceException {
		super.updateBefore(map);
		String distribute = ConvertUtil.convertToString(map.get("distribute"));
		if(StringUtil.isEmpty(distribute)) {
			return;
		}
		if(distribute.startsWith(",")){
			return;
		}
		distribute = ","+distribute+",";
		map.put("distribute",distribute);
	}
}
